﻿using Admin2024.Application.Contracts.User;
using Admin2024.Application.Contracts.User.Dto;
using Admin2024.Domain.Entity.System;
using Admin2024.Domain.Interface;
using Admin2024.Domain.ObjValue;
using Microsoft.AspNetCore.Identity.Data;
using Microsoft.AspNetCore.Mvc;

namespace Admin2024.Api.Controller
{
    [ApiController]
    public class AppUserController : ControllerBase
    {
        private readonly IUserServer userServer;

        private readonly IUserInfoService userInfoService;

        private readonly IUserStateService userStateService;

        private readonly IPasswordService passwordService;

        private readonly IAppUserRoleService appUserRoleService;
        public AppUserController(IUserServer userServer, IUserInfoService userInfoService, IUserStateService userStateService,IPasswordService passwordService,IAppUserRoleService appUserRoleService)
        {
            this.userServer = userServer;
            this.userInfoService = userInfoService;
            this.userStateService = userStateService;
            this.passwordService = passwordService;
            this.appUserRoleService = appUserRoleService;
        }
        //登入
        [HttpPost("/api/Login")]
        public async Task<DomainResult<LoginSucessDto>> Log(LoginRequestDto regSucceedDto)
        {
            return await userServer.Login(regSucceedDto.UserName, regSucceedDto.PassWord);
        }
        //注册
        [HttpPost("/api/Register")]
        public async Task<DomainResult<RegSucceedDto>> Reg(RegCreateDto regCreateDto)
        {
            return await userServer.Register(regCreateDto.username, regCreateDto.password, regCreateDto.ConfirmPassword);
        }

        //获取全部用户
        [HttpGet("/api/GetAllPage")]
        public async Task<DomainResult<List<AppUserDto>>> GetAllPage(int page, int pageSize)
        {
            return await userInfoService.GetUsersWithPagination(page, pageSize);
        }
       //根据token获取指定用户
        [HttpGet("/api/tokenUserInfo")]
        public async Task<DomainResult<UserInfoDto>> tokenUserInfo(string token)
        {
           return await userInfoService.GetUserInfo(token);
        }
        //获取指定用户
        [HttpGet("/api/GetById/{Id}")]
        public async Task<DomainResult<AppUserDto>> GetById(Guid Id)
        {
            return await userInfoService.GetUserById(Id);
        }
        //删除用户
        [HttpDelete("/api/DelUser/{Id}")]
        public async Task<DomainResult<UserDeleteDto>> Del(Guid Id)
        {
            return await userStateService.DeleteUserAsync(Id);
        }
        //批量删除
        [HttpPost("/api/DeleteRange")]

        public async Task<DomainResult<IEnumerable<string>>> DeleteRangeAsync(IEnumerable<Guid> ids)
        {
            return await userStateService.DeleteRangeAsync(ids);
        }
        //删除用户（软）
        [HttpPut("/api/Disable/{Id}")]
        public async Task<DomainResult<Guid>> Disable(Guid Id)
        {
            return await userStateService.DisableUserAsync(Id);
        }
        //禁用 / 启用 用户
        [HttpPut("/api/Enable/{Id}")]
        public async Task<DomainResult<Guid>> Enable(Guid Id)
        {
            return await userStateService.EnableUserAsync(Id);
        }
        //修改用户信息
        [HttpPut("/api/UpdateUser/{Id}")]
        public async Task<DomainResult<AppUserDto>> PutUser(Guid Id ,UserUpdateDto userUpdateDto)
        {
            return await userInfoService.UpdateUserProfile(Id, userUpdateDto);
        }

        //修改密码
        [HttpPut("/api/UpdatePassWord/{Id}")]
        public async Task<DomainResult<string>> UpdatePassword(Guid Id, string password) 
        {
            return await passwordService.UpdatePassword(Id, password);
        }

        //给用户分配角色
        [HttpPost("/api/AssignRoleToUser")]
        public async Task<DomainResult<string>> AssignRoleToUser(Guid appUserId,Guid appRoleId)
        {
           return await appUserRoleService.AssignRoleToUser(appUserId,appRoleId);
        }

        //删除用户角色
        [HttpDelete("/api/RemoveRoleFromUser")]
        public async Task<DomainResult<string>> RemoveRoleFromUser(Guid appUserId, Guid appRoleId)
        {
            return await appUserRoleService.AssignRoleToUser(appUserId, appRoleId);
        }


    } 
}

